package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import zhoug.logging.Logger;
import zhoug.common.utils.ToastUtils;
import zhoug.zui.menu.tree.ITreeNode;
import zhoug.zui.menu.tree.TreeNode;
import zhoug.zui.menu.tree.ZUITreeMenu;
import zhoug.demo.zui.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import androidx.appcompat.app.AppCompatActivity;

/**
 * 树形菜单页
 */
public class TreeMenuActivity extends AppCompatActivity {
    private static final String TAG = ">>>TreeMenuActivity";
    private static  boolean debug = false;

    private ZUITreeMenu mTreeMenu;
    private Random random = new Random();
    private TextView mTextSelected;
    private RadioButton mRadioYse;
    private RadioButton mRadioNo;
    private RadioGroup mRadioGroup;

    private boolean initChild = true;
    private CheckBox mCheckboxMulti;

    public static void setDebug(boolean debug) {
        TreeMenuActivity.debug = debug;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_menu);
        findViews();

        new Thread(() -> {
            List<TreeNode> list = loadData(1, "");
            mTreeMenu.post(() -> {
                mTreeMenu.setData(list);
            });

        }).start();
    }

    private void findViews() {
        mTextSelected = findViewById(R.id.text_selected);
        mTreeMenu = findViewById(R.id.treeMenu);
        mRadioYse = findViewById(R.id.radio_yse);
        mRadioNo = findViewById(R.id.radio_no);
        mRadioGroup = findViewById(R.id.radio_group);
        mCheckboxMulti = findViewById(R.id.checkbox_multi);

        mTreeMenu.setOnTreeMenuListener(new ZUITreeMenu.OnTreeMenuListener() {
            @Override
            public void onSelect(ZUITreeMenu.TreeMenuAdapter adapter, int position, ITreeNode treeNode) {
                mTextSelected.setText(treeNode.getName());
            }

            @Override
            public void onMultiSelectChange(ZUITreeMenu.TreeMenuAdapter adapter, int position, List<ITreeNode> selected, ITreeNode changeItem) {
                String s=null;
                if(selected.size()>0){
                    StringBuilder stringBuilder=new StringBuilder();
                    for (ITreeNode item : selected) {
                        stringBuilder.append(item.getName());
                        stringBuilder.append(",");
                    }
                    s=stringBuilder.toString();
                    if(s.length()>0){
                        s=s.substring(0,s.length()-1);
                    }
                }
                mTextSelected.setText(s);
            }

            @Override
            public void onExpand(ZUITreeMenu.TreeMenuAdapter adapter, int position, ITreeNode treeNode, boolean expand) {
                ToastUtils.toastShort((expand ? "展开:" : "收起:") + treeNode.getName());
            }

            @Override
            public void onExpandClick(ZUITreeMenu.TreeMenuAdapter adapter, int position, ITreeNode treeNode) {
                ToastUtils.toastShortCenter("动态加载child:" + treeNode.getName());
                loadChildByParent(adapter, (TreeNode) treeNode, position);
            }
        });


        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_yse) {
                    initChild = true;
                } else {
                    initChild = false;
                }
                new Thread(() -> {
                    List<TreeNode> list = loadData(1, "");
                    mTreeMenu.post(() -> {
                        mTreeMenu.setData(list);
                    });

                }).start();
            }
        });

        mCheckboxMulti.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTreeMenu.setMultiSelectEnable(isChecked);
                mTreeMenu.notifyDataSetChanged();
            }
        });
    }


    private List<TreeNode> loadData(int count, String parentId) {
        if (debug) {
        }
        List<TreeNode> list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            TreeNode treeNode = new TreeNode();
            list.add(treeNode);
            if (parentId != null) {
                treeNode.setId(parentId + i);
            } else {
                treeNode.setId(i + "");
            }
            treeNode.setName("菜单" + treeNode.getId());

            if (initChild) {
                if (count <= 3) {
                    treeNode.setChild(loadData(count + 1, treeNode.getId()));
                }
            } else {
                if (count <= 3) {
                    if (random.nextBoolean()) {
                        treeNode.setHasChild(true);
                    } else {
                        treeNode.setHasChild(false);
                    }
                }
            }


        }

        return list;
    }

    private void loadChildByParent(ZUITreeMenu.TreeMenuAdapter adapter, TreeNode treeNode, int position) {
        if (debug) {
            Logger.d(TAG, "loadChildByParent:");
        }
        new Thread(() -> {
            try {
                Thread.sleep(random.nextInt(200));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<TreeNode> child = null;
            if (random.nextBoolean()) {
                child = new ArrayList<>();
                TreeNode node = new TreeNode();
                child.add(node);
                node.setId(treeNode.getId() + "" + 1);
                node.setName("菜单" + node.getId());
                node.setHasChild(true);

                node = new TreeNode();
                child.add(node);
                node.setId(treeNode.getId() + "" + 2);
                node.setName("菜单" + node.getId());
                node.setHasChild(false);

                node = new TreeNode();
                child.add(node);
                node.setId(treeNode.getId() + "" + 3);
                node.setName("菜单" + node.getId());
                node.setHasChild(true);
                List<TreeNode> treeNodes = new ArrayList<>();
                String id = node.getId();
                node.setChild(treeNodes);
                node = new TreeNode();
                treeNodes.add(node);
                node.setId(id + "" + 1);
                node.setName("菜单" + node.getId());
                node.setHasChild(true);
                node = new TreeNode();
                treeNodes.add(node);
                node.setId(id + "" + 2);
                node.setName("菜单" + node.getId());
                node.setHasChild(false);

            }
            final List<TreeNode> c = child;
            mTreeMenu.post(new Runnable() {
                @Override
                public void run() {
                    if (c == null || c.size() == 0) {
                        treeNode.setHasChild(false);
                        adapter.notifyItemChanged(position);
                    } else {
                        treeNode.setChild(c);
                        adapter.openExpand(treeNode);
                    }


                }
            });

        }).start();
    }


    private void print(List<TreeNode> list, int ceng) {
        if (list == null || list.size() == 0) return;
        String pre = "";
        for (int i = 0; i < ceng; i++) {
            pre += "——";
        }
        for (TreeNode treeNode : list) {
            Logger.d(TAG, pre + treeNode.getId() + "," + treeNode.getName());
            print(treeNode.getChild(), ++ceng);
        }
    }
}